const timeAlert = {};

// 时间窗口
timeAlert.starts = function () {
  const tmThreads = threads.start(function () {
    // 增加一个延时，等待UI初始化完成再进行执行
    sleep(50);
    ui.run(function () {
      // 初始化时间选择默认为true
      ui.timeAlert.checked = true;
    });

    // 时间悬浮窗
    timeAlert.window = floaty.window(
      <frame gravity="right">
        <card bg="#CCffffff">
          <text
            paddingLeft="10"
            paddingRight="10"
            paddingTop="5"
            paddingBottom="5"
            id="dateFloaty"
            textSize="16sp"
            textColor="#000000"
          />
        </card>
      </frame>
    );

    timeAlert.window.setPosition(device.width - 500, 0);

    timeAlert.window.dateFloaty.click(() => {
      timeAlert.window.setAdjustEnabled(!timeAlert.window.isAdjustEnabled());
    });

    // 时间悬浮窗
    tmThreads.setInterval(() => {
      //对控件的操作需要在UI线程中执行
      ui.run(function () {
        timeAlert.window.dateFloaty.setText(dynamicText());
      });
    }, 50);

    function dynamicText() {
      const tbDate =
        new Date().getTime() +
        (Number(Api.tMailTimePlus) ? Number(Api.tMailTimePlus) : 0) -
        Number(Api.tMailNetWorkDelay);
      const tbRes = new Date(tbDate).Format("hh:mm:ss.S");
      const jdDate =
        new Date().getTime() +
        (Number(Api.jdTimePlus) ? Number(Api.jdTimePlus) : 0) -
        Number(Api.jdNetWorkDelay);
      const jdRes = new Date(jdDate).Format("hh:mm:ss.S");
      return "淘宝：" + tbRes + "\n" + "京东：" + jdRes;
    }
  });
};

module.exports = timeAlert;
